﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;

using Sumen.Model.Model;

namespace Sumen.Business.Presenter.Category
{
    public class CategoryPresenter
    {


        public int Update(object pObjectValue, object pId)
        {
            category newCategory = pObjectValue as category;
            using (sumenEntities obj = new sumenEntities())
            {
                int id = Convert.ToInt32(pId);
                category tempCategory = obj.categories.First(p => p.Cat_ID == id);
                tempCategory.Cat_Code = newCategory.Cat_Code;
                tempCategory.Cat_Title = newCategory.Cat_Title;
                tempCategory.Cat_Alias = newCategory.Cat_Alias;
                tempCategory.Cat_Path = newCategory.Cat_Path;
                tempCategory.Cat_Published = newCategory.Cat_Published;
                tempCategory.Cat_Featured = newCategory.Cat_Featured;
                tempCategory.Cat_Parent_id = newCategory.Cat_Parent_id;
                tempCategory.Cat_MetaDesc = newCategory.Cat_MetaDesc;
                tempCategory.Cat_MeteKey = newCategory.Cat_MeteKey;
                tempCategory.Cat_Actived = newCategory.Cat_Actived;
                return obj.SaveChanges();
            }
        }

        public int Add(object pObjectValue)
        {
            category newCategory = pObjectValue as category;
            using (sumenEntities obj = new sumenEntities())
            {
                obj.categories.Attach(newCategory);
                obj.Entry(newCategory).State = EntityState.Added;
                return obj.SaveChanges();
            }
        }

        public int Delete(object pObjectValue)
        {
            category newCategory = new category();
            newCategory.Cat_ID = Convert.ToInt32(pObjectValue);
            using (sumenEntities obj = new sumenEntities())
            {
                obj.categories.Attach(newCategory);
                obj.Entry(newCategory).State = EntityState.Deleted;
                return obj.SaveChanges();
            }
        }

        public ICollection<category> GetAll()
        {
            using (sumenEntities objEntities = new sumenEntities())
            {
                if (ClsPublic.SYSTEM_DISPLAY_ACTIVED == "1")
                {
                    return objEntities.categories.ToList();
                }
                else
                {
                    return (from p in objEntities.categories where p.Cat_Actived == 1 select p).ToList();
                }
            }
        }

        public ICollection<category> GetAllActived()
        {
            using (sumenEntities objEntities = new sumenEntities())
            {
                return (from p in objEntities.categories where p.Cat_Actived == 1 select p).ToList();
            }
        }

        public category GetById(object pId)
        {
            using (sumenEntities obj = new sumenEntities())
            {
                int id = Convert.ToInt32(pId);
                return obj.categories.FirstOrDefault(p => p.Cat_ID == id);
            }

        }
      
    }
}
